<?php

namespace app\admin\model;

use app\model\OilAgentConfig;
use fast\Random;
use think\facade\Db;
use app\common\model\Export;

class Agent
{
    private $table = 'agent';
    private $oilConfig = 'oil_agent_config';

    /**
     * 列表.
     */
    public function getList($params)
    {
        $where = [];
        $where[] = [$this->table . '.' . 'is_deleted', '=', 0];
        $username = trim($params['username'] ?? '');
        if ($username) {
            $where[] = [$this->table . '.username|' . $this->table . '.company_name', 'like', '%' . $username . '%'];
        }
        if (!empty($params['settle_type'])) {
            $where[] = ['settle_type', '=', $params['settle_type']];
        }
        if (!empty($params['status'])) {
            $where[] = ['status', '=', $params['status']];
        }
        if (isset($params['mobile_discount_fast']) && '' != $params['mobile_discount_fast']) {
            $where[] = ['mobile_discount_fast', '=', $params['mobile_discount_fast']];
        }
        if (isset($params['mobile_discount_slow']) && '' != $params['mobile_discount_slow']) {
            $where[] = ['mobile_discount_slow', '=', $params['mobile_discount_slow']];
        }
        // if (isset($params['recharge_amount_max']) && $params['recharge_amount_max'] != '' && isset($params['recharge_amount_min']) && $params['recharge_amount_min'] != '') {
        //     $where[] = ['recharge_amount', 'between', [$params['recharge_amount_min'] * 100, $params['recharge_amount_max'] * 100]];
        // }
        if (isset($params['recharge_amount_max']) && '' != $params['recharge_amount_max']) {
            $where[] = ['recharge_amount', '<=', $params['recharge_amount_max'] * 100];
        }
        if (isset($params['recharge_amount_min']) && '' != $params['recharge_amount_min']) {
            $where[] = ['recharge_amount', '>=', $params['recharge_amount_min'] * 100];
        }
        $createdDate = $params['createdDate'] ?? [];
        if (is_array($createdDate) && !empty($createdDate[0]) && $createdDate[1]) {
            $where[] = [$this->table . '.created_at', 'between time', [$createdDate[0], $createdDate[1] . ' 23:59:59']];
        }
        $paginate = [
            'page' => $params['page'] ?? 1,
            'list_rows' => $params['list_rows'] ?? 10,
        ];
        $sortFields = ['recharge_amount', 'balance', 'withdraw_amount', 'created_at'];
        if (!empty($params['sortField']) && !empty($params['sortOrder'])) {
            if (in_array($params['sortField'], $sortFields)) {
                if ('ascend' == $params['sortOrder']) {
                    $order = $this->table . '.' . $params['sortField'] . ' asc';
                } else {
                    $order = $this->table . '.' . $params['sortField'] . ' desc';
                }
            } else {
                $order = $this->table . '.id desc';
            }
        } else {
            $order = $this->table . '.id desc';
        }

        $fieldArr = [
            $this->table . '.id',
            $this->table . '.username',
            $this->table . '.avatar',
            $this->table . '.mobile',
            $this->table . '.balance',
            $this->table . '.withdraw_amount',
            $this->table . '.company_name',
            $this->table . '.settle_type',
            $this->table . '.status',
            $this->table . '.mobile_discount_slow',
            $this->table . '.mobile_discount_fast',
            $this->table . '.recharge_amount',
            $this->table . '.mobile_service',
            $this->table . '.oil_service',
            $this->table . '.ip_address',
            $this->table . '.created_at',
            'if(isNull(' . $this->oilConfig . '.commission_rate),"",' . $this->oilConfig . '.commission_rate' . ') as oil_commission_rate',
        ];
        $list = Db::name($this->table)
            ->leftJoin($this->oilConfig, $this->table . '.id=' . $this->oilConfig . '.agent_id')
            ->field($fieldArr)
            ->where($where)
            ->whereNull($this->oilConfig . '.deleted_at')
            ->order($order)
            ->paginate($paginate)
            ->toArray();

        return success('操作成功', $list);
    }

    /**
     * 添加.
     */
    public function create($params)
    {
        $unique = Db::name($this->table)
            ->where('username', $params['username'])
            ->where('is_deleted', 0)
            ->value('id');
        if ($unique) {
            return fail('该账号已经被占用');
        }
        $mobile = Db::name($this->table)
            ->where('mobile', $params['mobile'])
            ->where('is_deleted', 0)
            ->value('mobile');
        if ($mobile) {
            return fail('该手机号已经被占用');
        }

        $appKey = $this->getKey('app_key', 16);
        if (false === $appKey) {
            return fail('app_key 生成失败，请重试');
        }
        $appSecret = $this->getKey('app_secret', 32);
        if (false === $appSecret) {
            return fail('app_secret 生成失败，请重试');
        }
        if (empty($params['oil_service'])) {
            $params['oil_commission_rate'] = 0;
        }

        Db::startTrans();
        try {
            $id = Db::name($this->table)
                ->insertGetId([
                    'username' => $params['username'],
                    'password' => password_hash($params['password'], PASSWORD_DEFAULT),
                    'avatar' => '',
                    'mobile' => $params['mobile'],
                    'balance' => 0,
                    'withdraw_amount' => 0,
                    'company_name' => $params['company_name'] ?? '',
                    'mobile_discount_slow' => $params['mobile_discount_slow'] ?? 0,
                    'mobile_discount_fast' => $params['mobile_discount_fast'] ?? 0,
                    'oil_service' => $params['oil_service'] ?? 0,
                    'mobile_service' => $params['mobile_service'] ?? 0,
                    'status' => 1,
                    'settle_type' => $params['settle_type'],
                    'app_key' => $appKey,
                    'app_secret' => $appSecret,
                    'ip_address' => $params['ip_address'] ?? '',
                ]);

            // 加油返佣比例
            (new OilAgentConfig())->save([
                'agent_id' => $id,
                'commission_rate' => $params['oil_commission_rate'] ?? 0,
            ]);

            $mobileSupplierIds = $params['mobile_supplier_id'] ?? [];
            $insert = [];
            foreach ($mobileSupplierIds as $supplierId) {
                $insert[] = [
                    'agent_id' => $id,
                    'mobile_supplier_id' => $supplierId,
                ];
            }
            if ($insert) {
                Db::name('agent_mobile_supplier')
                    ->insertAll($insert);
            }

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();

            return fail('添加失败：' . $e->getMessage());
        }

        return success('添加成功', [
            'id' => intval($id),
        ]);
    }

    /**
     * 编辑.
     */
    public function edit($params)
    {
        $info = Db::name($this->table)
            ->field('mobile, app_key')
            ->where('id', $params['id'])
            ->where('is_deleted', 0)
            ->find();
        if (empty($info)) {
            return fail('代理商不存在');
        }
        if ($info['mobile'] != $params['mobile']) {
            $mobile = Db::name($this->table)
                ->where('mobile', $params['mobile'])
                ->where('is_deleted', 0)
                ->value('mobile');
            if ($mobile) {
                return fail('该手机号已经被占用');
            }
        }

        $update = [
            'mobile' => $params['mobile'],
            'company_name' => $params['company_name'] ?? '',
            'status' => $params['status'],
            'mobile_discount_slow' => $params['mobile_discount_slow'] ?? 0,
            'mobile_discount_fast' => $params['mobile_discount_fast'] ?? 0,
            'oil_service' => $params['oil_service'] ?? 0,
            'mobile_service' => $params['mobile_service'] ?? 0,
            'ip_address' => $params['ip_address'] ?? '',
        ];
        if (empty($params['oil_service'])) {
            $params['oil_commission_rate'] = 0;
        }

        if (empty($info['app_key'])) {
            $appKey = $this->getKey('app_key', 16);
            if (false === $appKey) {
                return fail('app_key 生成失败，请重试');
            }
            $appSecret = $this->getKey('app_secret', 32);
            if (false === $appSecret) {
                return fail('app_secret 生成失败，请重试');
            }
            $update['app_key'] = $appKey;
            $update['app_secret'] = $appSecret;
        }

        if (!empty($params['password'])) {
            $update['password'] = password_hash($params['password'], PASSWORD_DEFAULT);
        }

        $mobileSupplierIds = $params['mobile_supplier_id'] ?? [];
        $old = Db::name('agent_mobile_supplier')
            ->where('agent_id', $params['id'])
            ->column('mobile_supplier_id');
        $deleteIds = array_diff($old, $mobileSupplierIds);
        $insertIds = array_diff($mobileSupplierIds, $old);
        $insert = [];
        foreach ($insertIds as $id) {
            $insert[] = [
                'agent_id' => $params['id'],
                'mobile_supplier_id' => $id,
            ];
        }

        Db::startTrans();
        try {
            Db::name($this->table)
                ->where('id', $params['id'])
                ->update($update);
            if (!empty($params['mobile_service'])) {
                DB::name('agent_channel')
                    ->where('agent_id', $params['id'])
                    ->where('mobile_discount_slow', '<', $params['mobile_discount_slow'])
                    ->update([
                        'mobile_discount_slow' => $params['mobile_discount_slow'],
                    ]);
                DB::name('agent_channel')
                    ->where('agent_id', $params['id'])
                    ->where('mobile_discount_fast', '<', $params['mobile_discount_fast'])
                    ->update([
                        'mobile_discount_fast' => $params['mobile_discount_fast'],
                    ]);
            }
            if ($deleteIds) {
                Db::name('agent_mobile_supplier')
                    ->where('agent_id', $params['id'])
                    ->whereIn('mobile_supplier_id', $deleteIds)
                    ->delete();
            }
            if ($insert) {
                Db::name('agent_mobile_supplier')
                    ->insertAll($insert);
            }

            if (OilAgentConfig::where('agent_id', $params['id'])->find()) {
                OilAgentConfig::withTrashed()
                    ->where('agent_id', $params['id'])
                    ->save([
                        'deleted_at' => null,
                        'commission_rate' => $params['oil_commission_rate'] ?? 0,
                    ]);
            } else {
                (new OilAgentConfig())->save([
                    'agent_id' => $params['id'],
                    'commission_rate' => $params['oil_commission_rate'] ?? 0,
                ]);
            }

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();

            return fail('更新失败：' . $e->getMessage());
        }

        return success('更新成功', [
            'id' => intval($params['id']),
        ]);
    }

    /**
     * 充值
     */
    public function recharge($params)
    {
        Db::startTrans();
        try {
            $agent = Db::name($this->table)
                ->where('id', $params['id'])
                ->where('is_deleted', 0)
                ->lock(true)
                ->find();
            if (empty($agent)) {
                Db::rollback();
                return fail('代理商不存在');
            }
            // if ($params['amount'] < 100) {
            //     Db::rollback();
            //     return fail('充值金额最少 100 元');
            // }
            $amount = yuan2fen($params['amount']);
            if ($amount < 0 && ($amount + $agent['balance'] < 0)) {  // 提现，判断账户余额
                Db::rollback();
                return fail('代理商账户余额不足');
            }
            if ($amount > 100000000) {
                Db::rollback();
                return fail('充值金额最多 100 万元');
            }

            $newBalance = $agent['balance'] + $amount;
            if ($amount < 0) {
                $remark = $params['remark'] ?? ('后台提现 ' . abs($params['amount']) . ' 元');
                Db::name($this->table)
                    ->where('id', $params['id'])
                    ->dec('balance', abs($amount))
                    ->inc('withdraw_amount', abs($amount))
                    ->update([
                        'balance_last_updated_at' => date('Y-m-d H:i:s'),
                    ]);
            } else {
                $remark = $params['remark'] ?? ('后台充值 ' . $params['amount'] . ' 元');
                Db::name($this->table)
                    ->where('id', $params['id'])
                    ->inc('balance', $amount)
                    ->inc('recharge_amount', $amount)
                    ->update([
                        'balance_last_updated_at' => date('Y-m-d H:i:s'),
                    ]);
            }
            Db::name('agent_balance_log')
                ->insert([
                    'agent_id' => $params['id'],
                    'type' => BALANCE_AGENT_RECHARGE,
                    'amount' => $amount,
                    'old_balance' => $agent['balance'],
                    'new_balance' => $newBalance,
                    'remark' => $remark,
                    'quote_id' => 0,
                ]);

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();

            return fail('操作失败：' . $e->getMessage());
        }

        return success('操作成功');
    }

    /**
     * 删除.
     */
    public function delete($params)
    {
        if (empty($params['id'])) {
            return fail('参数有误');
        }
        $info = Db::name($this->table)
            ->where('id', $params['id'])
            ->where('is_deleted', 0)
            ->find();
        if (empty($info)) {
            return success('删除成功');
        }

        Db::startTrans();
        try {
            Db::name($this->table)
                ->where('id', $info['id'])
                ->update([
                    'is_deleted' => 1,
                ]);

            // 删除加油代理商的配置
            OilAgentConfig::where('agent_id', $params['id'])->delete();

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();

            return fail('删除失败: ' . $e->getMessage());
        }

        return success('删除成功');
    }

    /**
     * 资金明细.
     */
    public function getBalanceList($params)
    {
        $where = $this->setBalanceWhere($params);
        $paginate = [
            'page' => $params['page'] ?? 1,
            'list_rows' => $params['list_rows'] ?? 10,
        ];
        $typeList = config('app.balanceTypeList');
        $agentList = Db::name('agent')
            ->where('is_deleted', 0)
            ->column('username, company_name', 'id');
        $db = Db::name('agent_balance_log')
            ->where($where)
            ->order('id desc');
        if (isset($params['is_export']) && 1 == $params['is_export']) {
            //返回导出excel 需要的数据
            $list['data'] = $db->select()->toArray();
        } else {
            $list = $db->paginate($paginate)->toArray();
        }
        foreach ($list['data'] as $k => $v) {
            $list['data'][$k]['type_name'] = $typeList[$v['type']] ?? '未知';
            $list['data'][$k]['agent_name'] = $agentList[$v['agent_id']]['username'] ?? '';
            $list['data'][$k]['company_name'] = $agentList[$v['agent_id']]['company_name'] ?? '';
        }
        $list['balanceTypeList'] = $typeList;
        if (isset($params['is_export']) && 1 == $params['is_export']) {
            return $list['data'];
        }

        return success('操作成功', $list);
    }

    //统计金额
    public function getBalanceCount($params)
    {
        //统计金额
        $where = $this->setBalanceWhere($params);
        $result['total_amount'] = $result['money'] = 0;
        if ($params['type'] > 0) {
            $totalChange = Db::name('agent_balance_log')->where($where)->sum('amount');
            $result['total_amount'] = abs($totalChange) / 100;
            switch ($params['type']) {
                case 5:
                    //话费充值返回正在充值的资金
                    $con[] = ['supplier_status', 'in', [1, 2]];
                    $con[] = ['status', '=', 2];
                    foreach ($where as $nk => $nv) {
                        if ($nv[0] != 'type') {
                            $con[] = $nv;
                        }
                    }
                    $result['money'] = Db::name('mobile_order')->where($con)->sum('amount');
                    $result['money'] = abs($result['money']) / 100;
                    break;
            }
        }

        return success('操作成功', $result);
    }

    //设置条件
    public function setBalanceWhere($params)
    {
        $where = [];
        if (!empty($params['agent_id'])) {
            $where[] = ['agent_id', '=', $params['agent_id']];
        }
        if (!empty($params['type'])) {
            $where[] = ['type', '=', $params['type']];
        }
        if (!empty($params['username'])) {
            $ids = Db::name('agent')->where('username|company_name', 'like', '%' . $params['username'] . '%')->column('id');
            $where[] = ['agent_id', 'in', $ids];
        }
        $createdDate = $params['createdDate'] ?? [];
        if (is_array($createdDate) && !empty($createdDate[0]) && $createdDate[1]) {
            $where[] = ['created_at', 'between time', [$createdDate[0], $createdDate[1] . ' 23:59:59']];
        }

        return $where;
    }

    /**
     * 生成 app_key, app_secret.
     */
    private function getKey($field, $len)
    {
        for ($i = 0; $i < 21; ++$i) {
            $str = Random::alnumlow($len);
            $id = Db::name('agent')
                ->where($field, $str)
                ->value('id');
            if (empty($id)) {
                return $str;
            }
        }

        return false;
    }

    /**
     * 话费供应商列表.
     */
    public function mobileSupplierList()
    {
        $supplierList = Db::name('mobile_supplier')
            ->field('id, name, discount_slow, discount_fast')
            ->where('is_deleted', 0)
            ->select()
            ->toArray();

        return success('操作成功', $supplierList);
    }

    /**
     * 关联的供应商id.
     */
    public function associationMobileSupplierIds($params)
    {
        if (empty($params['agent_id'])) {
            return fail('代理商 id 不能为空');
        }

        $checkIds = Db::name('agent_mobile_supplier')
            ->where('agent_id', $params['agent_id'])
            ->column('mobile_supplier_id');

        return success('操作成功', $checkIds);
    }


    /**
     * 支付宝扫码支付【呆萌价收款码】.
     */
    public function queryDmjOrderByTradeNo($params)
    {
        exit;

        $agent = Db::name('agent')
            ->where('id', $params['id'])
            ->where('is_deleted', 0)
            ->find();
        if (empty($agent)) {
            return fail('代理商不存在');
        }
        $tradeNo = trim($params['trade_no']);
        if (empty($tradeNo)) {
            return fail('交易单号不能为空');
        }

        $id = Db::name('agent_balance_log')
            ->where('trade_no', $tradeNo)
            ->value('id');
        if ($id) {
            return fail('交易已经存在');
        }

        // 调呆萌价接口，如果已经在呆萌价充值过，不再让充值
        $url = 'http://www.shop.dmjvip.com/web-dmhw/huiRecharge/query?remark=' . $tradeNo;
        $res = curlGet($url);
        $res = json_decode($res, true);
        if (!isset($res['result']) || $res['result'] != 1) {
            return fail($res['msgstr'] ?? '交易不存在');
        }

        if (empty($res['payTime'])) {
            return fail('交易时间有误');
        }
        if (empty($res['amount'])) {
            return fail('交易金额有误');
        }

        // 交易时间判断，早于上线时间的，不给用: 2021-10-22 14:30:00
        // $time = '2021-10-22 14:30:00';
        $time = '2021-11-09 13:45:00';
        if ($res['payTime'] < $time) {
            return fail('该笔交易无法在线充值，请联系商务充值');
        }

        // 交易金额
        $amount = yuan2fen($res['amount']);

        $lockKey = 'admin.agent.query_dmj_order_by_trade_no.' . $tradeNo;
        $lockVal = cache($lockKey);
        if ($lockVal) {
            return fail('操作频繁');
        }
        cache($lockKey, 1, 500);

        Db::startTrans();
        try {
            $balance = Db::name('agent')
                ->where('id', $params['id'])
                ->value('balance');
            Db::name('agent_balance_log')
                ->insert([
                    'agent_id' => $params['id'],
                    'type' => BALANCE_ONLINE_RECHARGE,
                    'amount' => $amount,
                    'old_balance' => $balance,
                    'new_balance' => $balance + $amount,
                    'remark' => '【呆萌价】支付宝扫码充值 ' . $res['amount'] . ' 元。支付宝订单号：' . $tradeNo,
                    'quote_id' => 1,
                    'trade_no' => $tradeNo,
                ]);
            Db::name('agent')
                ->where('id', $params['id'])
                ->inc('balance', $amount)
                ->inc('recharge_amount', $amount)
                ->update();

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();

            return fail('查询失败：' . $e->getMessage());
        }

        return success('充值成功');
    }

    /**
     * 每日资金明细
     */
    public function getDailyBalanceList($params)
    {
        $where = [];
        $keyword = trim($params['keyword'] ?? '');
        if ($keyword) {
            $where[] = ['username|company_name', 'like', '%' . $keyword . '%'];
        }
        if (empty($params['dateymd'])) {
            $params['dateymd'] = [
                date('Y-m-d', strtotime('-7 days')),
                date('Y-m-d', strtotime('-1 days')),
            ];
        }
        $days = ceil((strtotime($params['dateymd'][1]) - strtotime($params['dateymd'][0])) / 86400);
        if ($days > 31) {
            return fail('时间跨度不能超过 31 天');
        }

        $paginate = [
            'page' => $params['page'] ?? 1,
            'list_rows' => $params['list_rows'] ?? 10,
        ];

        if (!empty($params['is_export'])) {
            $list = Db::name('agent')
                ->field('id, username, mobile, balance, profit_balance, company_name')
                ->where($where)
                ->order('id desc')
                ->select()
                ->toArray();
            if (empty($list)) {
                return fail('没有可导出的数据');
            }
            $list['data'] = $list;
        } else {
            $list = Db::name('agent')
                ->field('id, username, mobile, balance, profit_balance, company_name')
                ->where($where)
                ->order('id desc')
                ->paginate($paginate)
                ->toArray();
            if (empty($list)) {
                return success('操作成功', []);
            }
        }

        $agentIds = array_column($list['data'], 'id');
        $tmp = Db::name('agent_daily_balance')
            ->field('agent_id, dateymd, balance, profit_balance')
            ->whereIn('agent_id', $agentIds)
            ->whereTime('dateymd', 'between', $params['dateymd'])
            ->order('dateymd asc')
            ->select()
            ->toArray();
        $balanceList = [];
        foreach ($tmp as $v) {
            $balanceList[$v['agent_id']][$v['dateymd']] = [
                'balance' => $v['balance'],
                'profit_balance' => $v['profit_balance'],
            ];
        }
        unset($tmp);

        $dateArr = range(strtotime($params['dateymd'][0]), strtotime($params['dateymd'][1]), 86400);
        $defaultBalance = [];
        foreach ($dateArr as $k => $v) {
            $date = date('Y-m-d', $v);
            $dateArr[$k] = $date;
            $defaultBalance[$date] = [
                'balance' => 0,
                'profit_balance' => 0,
            ];
        }

        if (!empty($params['is_export'])) {
            $res = [];
            $title = array_merge(['代理商名称', '公司名称', '当前账户余额'], $dateArr);
            foreach ($list['data'] as $v) {
                $tmp = [
                    $v['username'] . "\t",
                    $v['company_name'] . "\t",
                    fen2yuan($v['balance']),
                ];
                foreach ($dateArr as $date) {
                    $tmp[] = fen2yuan($balanceList[$v['id']][$date]['balance'] ?? 0);
                }

                $res[] = $tmp;
            }
            unset($list);
            $export = new Export;
            $name = '代理商每日余额明细【' . $params['dateymd'][0] . '~' . $params['dateymd'][1] . '】';
            return $export->downloadExcel($title, $res, $name);
        }

        $list['dateArr'] = $dateArr;
        foreach ($list['data'] as $k => $v) {
            $list['data'][$k] = array_merge($v, $balanceList[$v['id']] ?? $defaultBalance);
        }

        return success('操作成功', $list);
    }

    /**
     * 代理商关联的供应商列表
     */
    public function relatedMobileSupplierList($params)
    {
        if (empty($params['agent_id'])) {
            return fail('代理商 id 不能为空');
        }

        $supplierIds = Db::name('agent_mobile_supplier')
            ->where('agent_id', $params['agent_id'])
            ->column('mobile_supplier_id');
        if (empty($supplierIds)) {
            return success('操作成功', [
                'supplier_list' => [],
                'mobile_supplier_ids' => [],
            ]);
        }

        $supplierList = Db::name('mobile_supplier')
            ->field('id, name')
            ->whereIn('id', $supplierIds)
            ->where('is_deleted', 0)
            ->select()
            ->toArray();

        $mobileSupplierIds = Db::name('agent')
            ->where('id', $params['agent_id'])
            ->value('mobile_supplier_ids');
        $mobileSupplierIds = json_decode($mobileSupplierIds, true) ?? [];
        foreach ($mobileSupplierIds as $k => $v) {
            if (!in_array($v, $supplierIds)) {
                unset($mobileSupplierIds[$k]);
            }
        }
        if (!empty($mobileSupplierIds)) {
            $mobileSupplierIds = array_merge([null], $mobileSupplierIds);
        }

        return success('操作成功', [
            'supplier_list' => $supplierList,
            'mobile_supplier_ids' => $mobileSupplierIds,
        ]);
    }

    /**
     * 保存话费轮询配置
     */
    public function updateMobilePolling($params)
    {
        if (empty($params['agent_id'])) {
            return fail('代理商id不能为空');
        }
        $res = Db::name('agent')
            ->where('id', $params['agent_id'])
            ->update([
                'mobile_supplier_ids' => json_encode($params['mobile_supplier_ids'] ?? []),
                'updated_at' => date('Y-m-d H:i:s'),
            ]);
        if ($res) {
            return success('配置成功');
        }
        return fail('配置失败，请刷新重试');
    }
}
